package self.hoy.demo.grainmall.product.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import self.hoy.demo.grainmall.product.service.SkuInfoService;
import self.hoy.demo.grainmall.product.vo.SkuItemVo;

import java.util.concurrent.ExecutionException;

/**
 * @author hyh
 * @date 2020/7/23
 */
@Controller
public class ItemController {

    @Autowired
    private SkuInfoService skuInfoService;

    /**
     * 初步压测（异步编排前）
     * 线程50
     * 样本11612
     * 吞吐量 172/sec
     * 最大值 808
     * 最小值 51
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/{skuId}.html")
    public String item(@PathVariable("skuId") Long skuId, Model model) throws ExecutionException, InterruptedException {
        SkuItemVo vo = skuInfoService.item(skuId);
        model.addAttribute("item", vo);
        return "item";
    }

    @GetMapping("/skuItemVo")
    @ResponseBody
    public SkuItemVo skuItemVo(@RequestParam("skuId") Long skuId, @RequestParam("way") String way) throws ExecutionException, InterruptedException {
        if ("future".equals(way)) {
            /**
             * 线程50
             * 样本20163
             * 吞吐量 595/sec
             * 最大值 327
             * 最小值 2
             */
            return skuInfoService.item(skuId);
        } else {
            /**
             * 线程50
             * 样本19793
             * 吞吐量 600/sec
             * 最大值 248
             * 最小值 2
             */
            return skuInfoService.itemNoFuture(skuId);
        }
    }
}
